﻿$(document).ready(function() {
    showList();
    loadCity();  // 加载城市列表
});

// 详细列表
function showList() {
    var parentid = $("#hParentID").val();

    $("#divList").html("<img src=\"../images/loading.gif\" />"); // 清空列表 
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_list&parentid=' + parentid,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            showListCallBack(json);
        }
    });
}

/*
功能: 将返回的json数据进行排版
json: json数据
*/
function showListCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0) {
        $("#divList").html("<font color='red'>&nbsp;系统内暂无您查询的信息!</font>");
        return;
    }

    var arr = [];
    arr.push("<table id='table1' width='100%' border='0' cellpadding='0' cellspacing='1' bgcolor='#a8c7ce'>");
    arr.push("<tr>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE6'>");
    arr.push("序号");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE6'>");
    arr.push("名称");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE6'>");
    arr.push("排序");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE6'>");
    arr.push("是否正常");
    arr.push("</td>");
    arr.push("<td height='25' bgcolor='d3eaef' class='STYLE6'>");
    arr.push("基本操作");
    arr.push("</td>");
    arr.push("</tr>");

    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        var isshow = "";
        if (record[i].dltFlag == 1) {
            isshow = "否";
        } else {
            isshow = "是";
        }

        if (i % 2 == 0)
            arr.push("<tr bgcolor='#FFFFFF'>");
        else
            arr.push("<tr bgcolor='#EEEDED'>");

        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].id);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].name);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(record[i].ord);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push(isshow);
        arr.push("</td>");
        arr.push("<td height='20' class='STYLE6'>");
        arr.push("<a href=\"javascript:add(\'" + record[i].id + "\',\'" + record[i].name + "\',\'" + record[i].parentid + "\',\'" + record[i].ord + "\',\'" + record[i].delflag + "\',\'" + record[i].grandid + "\')\">修改</a> | ");
        arr.push("<a href=\"javascript:del(\'" + record[i].id + "\')\">删除</a> ");
        arr.push("</td>");
        arr.push("</tr>");
    }
    arr.push("</table>");
    $("#divList").html(arr.join('')); // 显示列表
}

// 添加|修改
function add(id, name, parentid, ord, delflag, grandid) {
    openLayer("divList", "divLayer");

    if (id <= 0)
        return false;

    if (grandid > 0) {
        $("#sCity1").val(grandid);
        loadCity2();
        setTimeout("loadWait(" + parentid + ")", 100);
    } else {
        $("#sCity1").val(parentid);
        loadCity2();
    }

    $("#hID").val(id);
    $("#iCityName").val(name);
    $("#hParentID").val(parentid);
    $("#iOrd").val(ord);
    $("#hDltFlag").val(delflag);
    if (delflag == 1)
        document.getElementById("Radio2").checked = true;

}

function loadWait(parentid) {
    $("#sCity2").val(parentid);
}

// 加载城市列表
function loadCity() {
    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_list&parentid=0',
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            loadCityCallBack(json);
        }
    });
}

// 加载城市列表回调
function loadCityCallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0)
        return;

    var obj = document.getElementById("sCity1");
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        var sName = record[i].name;
        var sValue = record[i].id;

        LexunJS.selectAddOption(obj, sName, sValue);
    }
}

// 加载城区列表
function loadCity2() {
    var parentid = document.getElementById("sCity1").value;

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_list&parentid=' + parentid,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(json) {
            loadCity2CallBack(json);
        }
    });
}

// 加载城市列表回调
function loadCity2CallBack(response) {
    var json = eval("(" + response + ")");
    var record = json.record;

    if (record == null || record.length == 0)
        return;

    var obj = document.getElementById("sCity2");
    var recordLength = record.length;
    for (var i = 0; i < recordLength; i++) {
        var sName = record[i].name;
        var sValue = record[i].id;

        LexunJS.selectAddOption(obj, sName, sValue);
    }
}

// 删除
function del(id) {
    hiConfirm("您确定要删除该行，请谨慎操作！", "确认框", function(result) {
        if (result) {
            // ajax删除                   
            $.ajax({
                type: 'POST',
                url: '../Handler/HandlerIndex.ashx',
                data: 'act=area_del&id=' + id,
                timeout: '10000',
                error: function() {
                    alert("删除失败！");
                }, success: function(html) {
                    showList();
                }
            });
        }
    });
}

// 提交
function submitLayerOK() {
    var id = $("#hID").val();
    var name = $("#iCityName").val();

    var city1 = $("#sCity1").val();
    $("#hParentID").val(city1);
    var city2 = $("#sCity2").val();
    if (city2 > 0)
        $("#hParentID").val(city2);
    var parentid = $("#hParentID").val();

    var dltflag = 0;
    if (document.getElementById("Radio2").checked)
        dltflag = 1;

    var ord = $("#iOrd").val()

    if (name == "") {
        alert("名称不能为空！");
        return false;
    }

    closeLayer(); // 关闭层

    $.ajax({
        type: 'POST',
        url: '../Handler/HandlerIndex.ashx',
        data: 'act=area_save&id=' + id + '&parentid=' + parentid + "&name=" + name + "&dltflag=" + dltflag + "&ord=" + ord,
        timeout: '10000',
        error: function() {
            alert("修改失败！");
        }, success: function(outmsg) {
            alert(outmsg);
            showList();

            document.getElementById("hID").value = "";
            document.getElementById("hParentID").value = "";
        }
    });
}  
